﻿using FPC.Offer.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json;
namespace FPC.Offer.UI.GeneralProcessor.Role
{
    /// <summary>
    /// HandlerRole 的摘要说明
    /// </summary>
    public class HandlerRole : IHttpHandler
    {
        FPC.Offer.Service.System_RoleTableService Role = new Service.System_RoleTableService();
        FPC.Offer.Model.System_RoleTable mo = new Model.System_RoleTable();
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            var Actioon = context.Request["Actioon"].ToString();        
            if (Actioon == EnumAction.Select.ToString())
            {
                if (context.Request["GetId"] != null)
                {
                    var GetId=Convert.ToInt32(context.Request["GetId"].ToString());
                    context.Response.Write(JsonConvert.SerializeObject(Role.Find(t => t.RoleId == GetId)));
                }
                else 
                {
                    var RoleShow = Role.Find(null);
                    context.Response.Write(JsonConvert.SerializeObject(RoleShow));
                }
                context.Response.Write("");
            }
            //添加
            if (Actioon == EnumAction.Add.ToString())
            {
                if (context.Request["RoleDescription"] != null && context.Request["RoleName"] != null)
                {
                    mo.RoleDescription = context.Request["RoleDescription"].ToString();
                    mo.RoleName = context.Request["RoleName"].ToString();
                }
                context.Response.Write(Role.Add(mo));
            }
            //修改
            if (Actioon == EnumAction.Update.ToString())
            {

                if (context.Request["GetId"] != null && context.Request["RoleName"] != null && context.Request["RoleDescription"] != null)
                {
                    mo.RoleDescription = context.Request["RoleDescription"].ToString();
                    mo.RoleName = context.Request["RoleName"].ToString();
                    mo.RoleId = Convert.ToInt32(context.Request["GetId"].ToString());
                }
                context.Response.Write(Role.Update(mo));
            }
            //删除
            if (Actioon == EnumAction.Delete.ToString())
            {               
                if (context.Request["Getid"] != null) {
                  if(context.Request["Getid"].ToString().IndexOf(",")!=0)
                  {
                      var GetId= context.Request["Getid"].ToString();
                      context.Response.Write(Role.RoleBatchDelete(GetId));
                  }
                  else
                  {
                      mo.RoleId = Convert.ToInt32(context.Request["Getid"].ToString());
                      context.Response.Write(Role.Delete(mo));
                  }
                }               
            }
            context.Response.Write("");
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}